package gui;

import java.awt.BorderLayout;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import localhost._8080.metaimage.Client;

public class UI extends JFrame implements WindowListener{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 5600359126016098384L;

	//SplitPane
	//TabPane
	public UI(){
		addWindowListener(this);
		BorderLayout bd = new BorderLayout();
		SplitPaneDemo2 p = new SplitPaneDemo2();
		JPanel pan = new JPanel();
		pan.setLayout(bd);
		pan.add(p,BorderLayout.CENTER);
		this.add(pan);
	}

	public static void createAndShowGUI() {
		 //Create and set up the window.
		   
		final JFrame f = new JFrame();
		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		//fenetre de connexion
		JPanel pan = new JPanel(false);
	    JLabel jl = new JLabel("Enter your ID :");
		final JTextField jta = new JTextField(20);
		JButton b = new JButton("Enter");
		b.addMouseListener(new MouseListener() {

			@Override
			public void mouseClicked(MouseEvent arg0) {
				
				Client.id = jta.getText();
				if(Client.p.logIn(Client.id) == true) {
					// lancer l'appli
					JFrame frame = new UI();
					frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
					frame.setExtendedState(JFrame.MAXIMIZED_BOTH );
					f.dispose();
					//Display the window.
					frame.setTitle("Image Managing Web Service");
					frame.pack();
					frame.setVisible(true);
				}
				jta.setText("");
			}
			
			
			public void mouseEntered(MouseEvent arg0) {				
			}
						
			public void mouseExited(MouseEvent arg0) {
			}			
			
			public void mousePressed(MouseEvent arg0) {
			}
			
			public void mouseReleased(MouseEvent arg0) {
			}
		});
		
		jta.addKeyListener(new KeyListener(){

			@Override
			public void keyPressed(KeyEvent arg0) {
				if (arg0.getKeyChar() == '\n'){
					Client.id = jta.getText();
					if(Client.p.logIn(Client.id) == true) {
						// lancer l'appli
						JFrame frame = new UI();
						
						frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
						frame.setExtendedState(JFrame.MAXIMIZED_BOTH );
						f.dispose();
						//Display the window.
						frame.setTitle("Image Managing Web Service");
						frame.pack();
						frame.setVisible(true);
					}
					jta.setText("");				
				}
				
			}

			@Override
			public void keyReleased(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void keyTyped(KeyEvent arg0) {
				// TODO Auto-generated method stub
				
			}});
		
		
	    pan.add(jl);
		pan.add(jta);
		pan.add(b);
	    f.add(pan);
	    f.setTitle("Image Managing Web Service");
	    f.setLocationRelativeTo(null);
	    f.pack();
	    f.setVisible(true);
	     
		/*JFrame frame = new UI();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setExtendedState(JFrame.MAXIMIZED_BOTH );
		    
		//Display the window.
		frame.pack();
		frame.setVisible(true);*/
	 }

	@Override
	public void windowActivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosed(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowClosing(WindowEvent arg0) {
		Client.p.logOut(Client.id);		
	}

	@Override
	public void windowDeactivated(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowDeiconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowIconified(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void windowOpened(WindowEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	
}
